package departmentServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.soap.SOAPPart;
import java.io.IOException;
import java.sql.SQLException;

import static java.lang.System.out;

@WebServlet(urlPatterns = "/addDepartment")
public class addDepartment extends HttpServlet {
    java.sql.Connection conn = null;
    java.sql.Statement sqlStmt = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String department_name=request.getParameter("department_name");
        String department_number=request.getParameter("department_number");

        response.setContentType("text/html;charset=UTF-8");

        try {
            Class.forName("org.h2.Driver");
            conn = java.sql.DriverManager.getConnection("jdbc:h2:~/jpa-demo-db;MODE=MYSQL;DB_CLOSE_DELAY=-1", "sa", "sa");
            sqlStmt = conn.createStatement();
            sqlStmt.executeUpdate(
                    "insert into part (name,pno)" + " values ('" + department_name + "','" + department_number + "')"
            );

        } catch (Exception ex) {
            out.println(ex.toString());
        }
        System.out.println("增加成功!");
        response.sendRedirect("html/departments.jsp");
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
